package com.qh.yyxt.service.Impl;

import com.qh.yyxt.entity.DelayThing;
import com.qh.yyxt.entity.Flag;
import com.qh.yyxt.entity.GetAndReturn;
import com.qh.yyxt.mapper.DelayThingMapper;
import com.qh.yyxt.mapper.FlagMapper;
import com.qh.yyxt.mapper.GetAndReturnMapper;
import com.qh.yyxt.pojo.BorrowThingDTO;
import com.qh.yyxt.pojo.BorrowThingInfoDTO;
import com.qh.yyxt.pojo.FlagDTO;
import com.qh.yyxt.service.DelayThingService;
import com.qh.yyxt.service.FlagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class DelayThingServiceImpl implements DelayThingService
{

    @Autowired
    private DelayThingMapper delayThingMapper;
    @Autowired
    private FlagMapper flagMapper;
    @Autowired
    private GetAndReturnMapper getAndReturnMapper;
    @Autowired
    private BorrowThingServiceImpl borrowThingService;
    @Override
    public String addApply(DelayThing delayThing)
    {
        if(delayThing!=null)
        {

            delayThing.setDelaystate("待审批");
            Flag flag=flagMapper.findById(delayThing.getFid());
            if(!flag.getState().equals("未归还"))
                return "必须是审批后、正在使用的物品，才能申请延时";
            if(delayThing.getDelaytime().compareTo(flag.getEndtime())<0)
            {
                return "操作失败，延时后的时间和原来结束时间矛盾！";
            }
              if(delayThingMapper.add(delayThing)==1)
              {
                  return "操作成功！";
              }
        }
        return "操作失败！数据不合理！";
    }
    @Override
    public String update(DelayThing delayThing)
    {
        int vFlag=1;
        if(delayThing==null) return "操作失败！";
        //如果是审批通过了的
        if(delayThing.getDelaystate().equals("审批通过"))
        {
            DelayThing delayThing1=delayThingMapper.findById(delayThing.getId());
            //需要把时间修改了！
            vFlag=flagMapper.updateEndTime(delayThing1.getFid(),delayThing1.getDelaytime());
        }
        if(delayThingMapper.update(delayThing)==1&&vFlag==1)
        {
            return "操作成功！";
        }
        return "操作失败！";
    }
    @Override
    public List<FlagDTO> findList()
    {
        List<FlagDTO>dtoList=new ArrayList<>();
        //先查出来所有的延时的fid
        List<DelayThing>list=delayThingMapper.findAll();
        for (DelayThing delayThing:list)
        {
            Flag flag =flagMapper.findById(delayThing.getFid());
            if(flag==null) continue;
            FlagDTO flagDTO=new FlagDTO();
            flagDTO.setFlagDTO(flag,null,getAndReturnMapper.findByFid(flag.getId()));
            flagDTO.setDelayThing(delayThing);
            dtoList.add(flagDTO);
        }
        return dtoList;
    }

    @Override
    public List<FlagDTO> findListByUser(Integer uId)
    {
        List<FlagDTO>dtos=new ArrayList<>();
        //先获取到所有的和用户相关的flag
        List<Flag> flags=flagMapper.findByUid(uId);
        if(flags.isEmpty()) return dtos;
        for (Flag flag:flags)
        {
            DelayThing delayThing=delayThingMapper.findByFid(flag.getId());
            if (delayThing!=null)
            {
                FlagDTO flagDTO=new FlagDTO();
                flagDTO.setFlagDTO(flag,null,getAndReturnMapper.findByFid(flag.getId()));
                flagDTO.setDelayThing(delayThing);
                dtos.add(flagDTO);
            }
        }
        return dtos;
    }

    @Override
    public BorrowThingInfoDTO findDetail(Integer fid)
    {
        BorrowThingInfoDTO borrowThingInfoDTO=borrowThingService.findByFid(fid);
        borrowThingInfoDTO.setDelayThing(delayThingMapper.findByFid(fid));
        return borrowThingInfoDTO;
    }
}
